﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using MI.ORM.Mapping;

namespace MI.ORM
{
    public class FluentNhibernateHelper
    {
        private static ISessionFactory _sessionFactory { get; set; }

        public ISessionFactory SessionFactory
        {
            get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); }
        }
        public static ISessionFactory GetCurrentFactory()
        {
            if (_sessionFactory == null)
            {
                _sessionFactory = CreateSessionFactory();
            }
            return _sessionFactory;
        }

        private static ISessionFactory CreateSessionFactory()
        {
            //return Fluently.Configure()
            //    .Database(
            //        FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005.ShowSql()
            //            .ConnectionString(s => s.Server("LYONTAM-PC\\SQL2005")
            //                .Database("GuestBook")
            //                .TrustedConnection()))
            //    .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(CategoryMap).Assembly).ExportTo("d:\\path"))
            //    .BuildSessionFactory();
            return Fluently.Configure()
             .Database(
             FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005.ConnectionString(
             c => c.FromConnectionStringWithKey("db")).ShowSql())
             .Mappings(x => x.FluentMappings.AddFromAssembly(typeof(CategoryMap).Assembly))
             .BuildSessionFactory();
        }
    }
}
